<?php
class M_tag {
  public function main() {
    if ( Site::$ajax ) {
      if ( !isset( $_GET['act'] ) ) $_GET['act']='';
      switch ( $_GET['act'] ) {
        case 'bind':
          if ( isset( $_GET['id'] ) && isset( $_GET['to'] ) ) {
            $sql = "UPDATE `tags_names` SET `tag`=':1' WHERE `id`=':2'";
            if ( db::q( $sql, $_GET['to'], $_GET['id'] )->ready() ) {
              $out = 'OK';
            } else {
              $out = 'ERROR';
            }
          }
          break;
        case 'unbind':
          if ( isset( $_GET['id'] ) ) {
            $sql = "UPDATE `tags_names` SET `tag`=':1' WHERE `id`=':1'";
            if ( db::q( $sql, $_GET['id'] )->ready() ) {
              $out = 'OK';
            } else {
              $out = 'ERROR';
            }
          }
          break;
        case 'edit':
          $id = isset($_GET['id']) ? (int) $_GET['id'] : 0;
          $sql = "UPDATE `tags_names` SET `name`=':2', `permalink`=':3' WHERE `id`=':1'";
          if ( db::q( $sql, $id, $_POST['name'], $_POST['permalink'] )->ready() ) {
            $out = 'OK';
          } else {
            $out = 'ERROR';
          }
          break;
        case 'add':
          if ( isset( $_POST['name'] ) ) {
            $sql = "SELECT MAX(`id`)+1 AS `id` FROM `tags_names`";
            $id = db::q( $sql )->get( 'id' );
            $p = str_replace( ' ', '_', $_POST['name'] );
            $sql = "
               INSERT INTO `tags_names` 
               (`id`, `name`, `permalink`, `tag`)
               VALUES
               (':1', ':2', ':3', ':1')";
            $query = "
               INSERT INTO `tags` 
               (`id`, `name`)
               VALUES 
               (':1', ':1')";
            if ( db::q( $sql, $id, $_POST['name'], $p )->ready() &&
                 db::q( $query, $id )->ready()  ) {
              $out = 'OK';
            } else {
              $out = 'ERROR';
            }
          }
          break;
        case 'del':
          $id = isset($_GET['id']) ? (int) $_GET['id'] : 0;
          $sql = "
             UPDATE `tags_names`
             SET `tag`=`id`
             WHERE `tag`=':1' ;;
             
             DELETE FROM `tags_relat` 
             WHERE `tag`=':1' ;;
             
             DELETE FROM `tags_names` 
             WHERE `id`=':1' ;;
             
             DELETE FROM `tags` 
             WHERE `id`=':1'";
          if ( db::q( $sql, $id )->ready()  ) {
            $out = 'OK';
          } else {
            $out = 'ERROR';
          }
          break;
        default:
          $id = isset($_GET['id']) ? (int) $_GET['id'] : 0;
          $list = new ListGeneric();
          $list->sql = "
            SELECT `tags_names`.`id`, `tags_names`.`name`, `tags_names`.`permalink`
            FROM `tags_names` 
            WHERE `tag` = '$id' AND `id` != '$id'
            ORDER BY `name`";
          $list->load();
          $list->template = 'pan_tag_form';
          $out = $list->get_html();
          if ( $id ) { 
            $sql = "SELECT * FROM `tags_names` WHERE `id`='$id'";
            $out->assign_r( db::q( $sql )->assoc() );
          }
          break;
      }
      echo $out;
    }
  }
}
?>